#--------- Date:2017-6-28 ---------
#Owner: baoshan.zhang
#Summary:Class used to fetch security daily's info

import pymssql
import pandas as pd
import pandas.io.sql as sql
from pandas import *
import numpy as np
import math
from common.statistic import *
from matplotlib import pyplot as plt
import logging
import logging.handlers

def log_setup():
    log_handler = logging.handlers.TimedRotatingFileHandler(filename=r'c:\log\automation.log', when='D', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
    formatter = logging.Formatter('\r\n\r\n--------------%(asctime)s : %(message)s------------------')
    log_handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(log_handler)
    logger.setLevel(logging.INFO)

class IndexSecurity:
    __security_id=''
    def __init__(self,securityId):
        try:
            self.conn = pymssql.connect(server='.', user='sa', password='qweqwe2', database='JYDB2')
            self.__security_id = securityId
            query = ' SELECT SM.ChiName,SM.ChiNameAbbr,I.ClosePrice,I.TradingDay FROM ..SecuMain SM JOIN ..QT_IndexQuote I ON SM.InnerCode=I.InnerCode' \
                    ' WHERE SM.SecuCode=\'%s\' AND SM.SecuCategory=\'4\' ORDER BY TradingDay' % securityId
            self.daily_quote_df = sql.read_sql(query, self.conn, index_col=['TradingDay'], parse_dates=['TradingDay'])

        except Exception as inst:
            print(inst)
            logging.exception("Error happened when initializing Security " + str(self.__security_id))



if __name__ == '__main__':
    data = IndexSecurity('000300')
    print(data.daily_quote_df[:10])






